<?php
    include_once("../action/checkAuthenticationAction.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Quản lý banner sản phẩm mới</title>
	<?php 
        include("import-css.php");
        include("../util/constant.php");
        include("../util/message.php");
		include_once("../util/authUtil.php");
    ?>
    
</head>
<body>
	<?php include("header.php");?>
<form id="form" method="post">
	<div class="container-fluid">
		<div class="row-fluid">
			<?php 
                include("menu.php");
                include_once("../service/bannerService.php");
            ?>
			<div id="content" class="span10">
            	<div id="msg" class="center alert"></div>
                <div class="row-fluid sortable">
    				<div class="box span12">
    					<div class="box-content">
                            
                            <table class="table table-striped table-bordered bootstrap-datatable datatable">
                                <thead>
                                    <tr>
                                        <th>STT</th>
                                        <th>Mã</th>
                                        <th>Tên</th>
                                        <th>Trạng thái</th>
                                        <th>Chức năng</th>
                                    </tr>
                                 </thead>   
                                 <tbody>
                                    <?php
                                        $newProductBanners = findAllNewProductBanners(1);
                                        $count = 1;
                                        while($row = ConnectionManager::getInstance()->fetch($newProductBanners)) {
											$productData = preg_split("/,/", $row["PRODUCT"]);
                                    ?>
                                            <tr> 
                                                <td><?php echo $count++;?></td>
                                                <td><?php echo $productData[0];?></td>
                                                <td><?php echo $productData[1];?></td>
                                                <td>
                                                	<select name="status" <?php if(authUtil(array("edit new_product_banner"),$perm_array,true)){?>onChange="updateStatus(<?php echo $row["BANNER_ID"];?>, this.value)"<?php }?>>
                                                    	<?php
															if($row["BANNER_STATUS"] == 0){
                                                        ?>
                                                                <option value="1">Bật</option>
                                                                <option value="0" selected>Tắt</option>
                                                        <?php
															} else {
                                                        ?>
                                                        		<option value="1" selected>Bật</option>
                                                                <option value="0">Tắt</option>
                                                        <?php
															}
                                                        ?>
                                                    </select>
												</td>                                                
                                                <td>
                                                	<?php
														if(authUtil(array("edit new_product_banner"),$perm_array,true)){
                                                    ?>
                                                        <a class="btn btn-info" href="new-product-banner-detail.php?action=<?php echo EDIT_ACTION; ?>&id=<?php echo $row["BANNER_ID"];?>">
                                                            <i class="icon-edit icon-white"></i>  
                                                            Cập nhật                                            
                                                        </a>
                                                    <?php
														}
                                                    ?>
                                                </td>
                                            </tr>
                                    <?php
                                        }
                                    ?>
                                 </tbody>
                             </table>    
    					</div>
    				</div><!--/span-->
    			</div><!--/row-->
            </div><!-- end of content -->
        </div><!-- end of row-fluid -->
        <?php include("footer.php");?>
    </div><!-- container-fluid -->
    <?php include("import-js.php");?>
    <script>
		function updateStatus(bannerId, status){
            $.ajax({
                type: "POST",
                url: "../action/ajax/updateNewProductBannerStatusProcess.php",
                data: { bannerId : bannerId, status : status },
                success: function(data){
                    if(data == <?php echo UPDATE_SUCCESSFULLY;?>){
                        showMessage("<?php echo $messages["editNewProductBannerStatusSuccessfully"];?>", "success");
					} else {
						showMessage("<?php echo $messages["editNewProductBannerStatusFailed"];?>", "error");	
					}
                }
            });
		}
	</script>
</form>
</body>
</html>